package se_channeltemplate

import (
	"fmt"
	"wx-api/adx-api/cache"
	"wx-api/adx-api/errmsg"
	"wx-api/adx-api/handlers"
	"wx-api/adx-api/model/m_channeltemplate"
	"wx-api/adx-api/services/comm"
	"wx-api/adx-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		return
	}
	data, err := m_channeltemplate.List(id, name, "", flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	tmplist := []m_channeltemplate.ChannelTemplate{}
	ttt := data.Data.(*[]m_channeltemplate.ChannelTemplate)
	for _, v := range *ttt {
		tmp := v
		sinfo, ok := cache.CacheChannelListMapNGet(v.DspId)
		if ok {
			tmp.DspName = sinfo.Name
		}
		tmplist = append(tmplist, tmp)
	}
	data.Data = tmplist
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_channeltemplate.ChannelTemplate
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		return
	}
	if form.DspId == "" {
		c.SetErrorMsg(nil, errmsg.GetError("所属dsp is error"))
		return
	}
	dspinfo, ok := cache.CacheChannelListMapGet(form.DspId)
	if !ok {
		c.SetErrorMsg(nil, errmsg.GetError("所属dsp is error"))
		return
	}
	form.DspId = dspinfo.Id.Hex()
	form.TemplateId = utils.RandomString(6)
	if form.CreateTemplateId == "" {
		c.SetErrorMsg(nil, errmsg.GetError("第三方模版id不能为空"))
		return
	}
	slot_type_ck := utils.CkslotType(form.SlotType)
	if form.SlotType == "" || !slot_type_ck {
		c.SetErrorMsg(nil, errmsg.GetError("广告位类型选择错误"))
		return
	}
	id, err := m_channeltemplate.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheChannelTemplateMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_channeltemplate.ChannelTemplate
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		return
	}
	sspinfo, err := m_channeltemplate.GetByCk(form.Id.Hex(), "")
	if err != nil || sspinfo == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	err = m_channeltemplate.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheChannelTemplateMapUpdate(sspinfo.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		return
	}
	data, err := m_channeltemplate.GetByCk(id, "")
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		return
	}
	data, err := m_channeltemplate.GetByCk(id, "")
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_channeltemplate.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheChannelTemplateMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		return
	}
	data, err := m_channeltemplate.GetByCk(id, "")
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_channeltemplate.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheChannelTemplateMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

// 获取所有所属模版
func TempAll(c *handlers.Controller) {
	dsp_uid := comm.GetUserInfo(c)
	if dsp_uid == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无缓存，请联系管理员"))
		return
	}
	dsp_id := c.GetString("dsp_id")
	tmpeids := cache.CacheChannelTemplateResDspIdGet(dsp_id)
	tem_arr := []m_channeltemplate.ChannelTemplate{}
	for _, id := range tmpeids {
		info, ok := cache.CacheChannelTemplateMapGet(id)
		if !ok {
			continue
		}
		tem_arr = append(tem_arr, m_channeltemplate.ChannelTemplate{
			Id:               info.Id,
			Name:             info.Name,
			TemplateId:       info.TemplateId,
			CreateTemplateId: info.CreateTemplateId,
			SlotType:         info.SlotType,
			Description:      info.Description,
		})
	}
	c.SetErrorMsg(tem_arr, errmsg.SuccessMsg())
	return
}
